﻿using Yxinmt.Interface;
using System;
using System.Collections.Generic;
using System.Windows.Controls;

namespace Yxinmt.Controls
{
    public class StackPanel : BaseControl, IControl
    {
        private System.Windows.Controls.StackPanel _StackPanel { get; set; }

        public StackPanel(Dictionary<string, object> property, IComponent component, bool isFactory = false)
        {
            Property = property;
            _Component = component;
            _IsFactory = isFactory;
            if (_IsFactory)
            {
                _FactoryPropertyList = new List<FactoryProperty>();
                _ControlType = typeof(System.Windows.Controls.StackPanel);
            }

            _StackPanel = new System.Windows.Controls.StackPanel();
            this.Element = _StackPanel;

            InitSet();
            this.SetProperty();
            this.SetControlProperty(false, false);
        }

        public override void Add(System.Windows.FrameworkElement control)
        {
            _StackPanel.Children.Add(control);
        }

        public override void Remove(System.Windows.FrameworkElement control)
        {
            _StackPanel.Children.Remove(control);
        }

        public override void Clear()
        {
            if (_StackPanel.Children.Count > 0) _StackPanel.Children.Clear();
        }

        private void SetProperty()
        {
        }

        protected override void SetProperty(string name, object value)
        {
            System.Windows.Controls.StackPanel c = _StackPanel;
            switch (name)
            {
                case "name": c.Name = string.Concat("spnl_", (string)value); SetFactoryProperty("Name", c.Name); break;
                case "Height": c.Height = Convert.ToDouble(value); SetFactoryProperty("Height", c.Height); break;
                case "Width": c.Width = Convert.ToDouble(value); SetFactoryProperty("Width", c.Width); break;
                case "Flow": c.FlowDirection = this.GetFlowDirection((string)value); SetFactoryProperty("FlowDirection", c.FlowDirection); break;
                case "VerticalAlignment": c.VerticalAlignment = GetVerticalAlignment((string)value); SetFactoryProperty("VerticalAlignment", c.VerticalAlignment); break;
                case "HorizontalAlignment": c.HorizontalAlignment = GetHorizontalAlignment((string)value); SetFactoryProperty("HorizontalAlignment", c.HorizontalAlignment); break;
                case "Orientation": c.Orientation = this.GetOrientation((string)value); SetFactoryProperty("Orientation", c.Orientation); break;
                case "IsHorizontal": c.Orientation = this.GetOrientation(Convert.ToBoolean(value) ? "Horizontal" : "Vertical"); SetFactoryProperty("Orientation", c.Orientation); break;
                case "Dock": System.Windows.Controls.DockPanel.SetDock(c, this.GetDock((string)value)); break;
                case "ControlAlign": SetControlAlign((string)value); break;
                case "GridRowColumn": SetGridRowColumn((string)value); break;
                case "Visible": SetVisible(Convert.ToBoolean(value)); break;
                case "BackColor": c.Background = GetColor((string)value); SetFactoryProperty("Background", c.Background); break;
                case "Margin": c.Margin = this.GetThickness((string)value); SetFactoryProperty("Margin", c.Margin); break;
                case "BackImageUrl": SetBackgroundImage((string)value); break;
                case "ZIndex": SetZIndex(Convert.ToInt32(value)); break;
                case "CanvasLocation": SetCanvasLocation((string)value); break;
                case "CanvasZIndex": SetCanvasZIndex(Convert.ToInt32(value)); break;
            }
        }

        protected override void SetFactoryProperty(string name, object value)
        {
            if (!_IsFactory) return;

            switch (name)
            {
                case "Margin": this.AddFactoryProperty(System.Windows.Controls.StackPanel.MarginProperty, value); break;
                case "Name": this.AddFactoryProperty(System.Windows.Controls.StackPanel.NameProperty, value); break;
                case "Height": this.AddFactoryProperty(System.Windows.Controls.StackPanel.HeightProperty, value); break;
                case "Width": this.AddFactoryProperty(System.Windows.Controls.StackPanel.WidthProperty, value); break;
                case "FlowDirection": this.AddFactoryProperty(System.Windows.Controls.StackPanel.FlowDirectionProperty, value); break;
                case "VerticalAlignment": this.AddFactoryProperty(System.Windows.Controls.StackPanel.VerticalAlignmentProperty, value); break;
                case "HorizontalAlignment": this.AddFactoryProperty(System.Windows.Controls.StackPanel.HorizontalAlignmentProperty, value); break;
                case "Orientation": this.AddFactoryProperty(System.Windows.Controls.StackPanel.OrientationProperty, value); break;
                case "Visibility": this.AddFactoryProperty(System.Windows.Controls.StackPanel.VisibilityProperty, value); break;
                case "Background": this.AddFactoryProperty(System.Windows.Controls.StackPanel.BackgroundProperty, value); break;
            }
        }
    }
}
